package com.neu.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.google.gson.Gson;
import com.neu.R;
import com.neu.bean.Result;
import com.neu.util.VolleyUtil;
import com.neu.view.App;

import java.util.HashMap;
import java.util.Map;

public class RegActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText edUserName;
    private EditText edNickName;
    private EditText edPhone;
    private EditText edEmail;
    private RadioButton rdMan;
    private RadioButton rdWoman;
    private RadioGroup radioGroup;
    private Button btnSubmit;
    private Button btnBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        initView();
    }

    private void initView() {
        edUserName = (EditText) findViewById(R.id.edUserName);
        edNickName = (EditText) findViewById(R.id.edNickName);
        edPhone = (EditText) findViewById(R.id.edPhone);
        edEmail = (EditText) findViewById(R.id.edEmail);
        rdMan = (RadioButton) findViewById(R.id.rdMan);
        rdWoman = (RadioButton) findViewById(R.id.rdWoman);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        btnSubmit = (Button) findViewById(R.id.btnSubmit);
        btnBack = (Button) findViewById(R.id.btnBack);

        btnSubmit.setOnClickListener(this);
        btnBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnSubmit:
                submit();
                break;
            case R.id.btnBack:
                Intent intent =new Intent(this,LoginActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }

    private void submit() {
        // validate
        String edUserNameString = edUserName.getText().toString().trim();
        if (TextUtils.isEmpty(edUserNameString)) {
            Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String edNickNameString = edNickName.getText().toString().trim();
        if (TextUtils.isEmpty(edNickNameString)) {
            Toast.makeText(this, "昵称不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String edPhoneString = edPhone.getText().toString().trim();
        if (TextUtils.isEmpty(edPhoneString)) {
            Toast.makeText(this, "电话不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if(edPhoneString.length()!=11){
            Toast.makeText(this, "电话必须是11位", Toast.LENGTH_SHORT).show();
            return;
        }

        String edEmailString = edEmail.getText().toString().trim();
        if (TextUtils.isEmpty(edEmailString)) {
            Toast.makeText(this, "Email不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something

        doRegist();
    }

    private void doRegist() {
        Map<String,String> map=new HashMap<>();
        map.put("avatar","/profile/2020/10/26/27e7fd58-0972-4dbf-941c-590624e6a886.png");
        map.put("userName",edUserName.getText().toString());
        map.put("nickName",edNickName.getText().toString());
        map.put("password","123456");
        map.put("phonenumber",edPhone.getText().toString());
        String sex="1";
        if(rdMan.isChecked()){//男被选中
            sex="0";
        }
        map.put("sex",sex);
        map.put("email",edEmail.getText().toString());
        map.put("idCard","210113199808242137");
        final String url="/prod-api/api/register";
        VolleyUtil.request(Request.Method.POST, url, map, new VolleyUtil.BackJson() {
            @Override
            public void success(String json) {
                Result result=new Gson().fromJson(json,Result.class);
                if(200==result.getCode()){
                    Toast.makeText(RegActivity.this,"注册成功",Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(RegActivity.this,result.getMsg(),Toast.LENGTH_LONG).show();
                }
                App.sRequsetQueue.getCache().remove(VolleyUtil.BASE_URL+url);
            }
        });
    }
}